package com.yx.frame.builder.xml;

import java.util.ArrayList;
import java.util.List;
import org.jdom.Element;

public class MethodXml {

	int id;
	String name = "";
	String type = "";
	String author = "";
	String comment = "";
	String pagination = "false";
	String date = "";
	String RemoveMemcachedUserLicense="false";
	String isMobilApp="false";
	String asyncService="false";

	MethodContentXml methodContentXml = null;

	List<MethodParmXml> methodParms = new ArrayList<MethodParmXml>();

	MethodMencacheXml methodMencacheXml = null;

	public MethodXml(Element method) throws Exception {

		this.id = Integer.parseInt(method.getAttributeValue("id").trim());
		this.name = method.getAttributeValue("name").trim();
		this.type = method.getAttributeValue("type").trim();
		this.comment = method.getAttributeValue("comment");
		this.author = method.getAttributeValue("author");
		this.date = method.getAttributeValue("date");
		this.pagination = method.getAttributeValue("pagination");
		try{
			this.RemoveMemcachedUserLicense= method.getAttributeValue("RemoveMemcachedUserLicense");
		}catch(Exception e){
			RemoveMemcachedUserLicense="false";
		}
		if(!"true".equals(RemoveMemcachedUserLicense)){
			RemoveMemcachedUserLicense="false";
		}
		try{
			this.isMobilApp= method.getAttributeValue("IsMobilApp");
		}catch(Exception e){
			isMobilApp="false";
		}
		if(!"true".equals(isMobilApp)){
			isMobilApp="false";
		}
		try{
			this.asyncService= method.getAttributeValue("AsyncService");
		}catch(Exception e){
			asyncService="false";
		}
		if(!"true".equals(asyncService)){
			asyncService="false";
		}	
		
		Element mencacheElement = method.getChild("Mencache");
		if (mencacheElement == null) {
			methodMencacheXml = new MethodMencacheXml("false","false", "0","0", "","false","false");
		} else {
			String cached = mencacheElement.getAttributeValue("cached");
			String staticfile = mencacheElement.getAttributeValue("staticfile");
			String catcheTimeout = mencacheElement.getAttributeValue("catcheTimeout");
			String staticfileDays = mencacheElement.getAttributeValue("staticfileDays");
			String key = mencacheElement.getAttributeValue("key");
			String isPrivate = mencacheElement.getAttributeValue("private");
			String isPagable = mencacheElement.getAttributeValue("page");
			methodMencacheXml = new MethodMencacheXml(cached,staticfile, catcheTimeout, staticfileDays,key,isPrivate,isPagable);
		}
		Element parmsElement = method.getChild("Parms");
		List<Element> parms = parmsElement.getChildren();
		for (int i = 0; i < parms.size(); i++) {
			Element parm = parms.get(i);
			int pId = Integer.parseInt(parm.getAttributeValue("id"));
			String pName = parm.getAttributeValue("name");
			String pType = parm.getAttributeValue("type");
			String pIsMutilist = parm.getAttributeValue("isMutilist");
			String pSqlIndex = parm.getAttributeValue("sqlIndex");
			String pValidator = parm.getAttributeValue("validator");
			String pComment = parm.getAttributeValue("comment");
			MethodParmXml methodParmXml = new MethodParmXml(pId, pName, pType,
					pIsMutilist, pSqlIndex, pValidator, pComment);
			this.getMethodParms().add(methodParmXml);
		}
		sortMethodParm();
		if ("query".equals(getType())) {
			Element transactionElement = method.getChild("Transaction");
			String callback=transactionElement.getAttributeValue("callback");
			if(callback==null)callback="";			
			methodContentXml = new QueryMethodContentXml();
			((QueryMethodContentXml) methodContentXml).setCallback(callback);
			List<Element> transactions = transactionElement.getChildren();
			for (int i = 0; i < transactions.size(); i++) {
				Element sqlElement = transactions.get(i);
				String sql = sqlElement.getValue();
				String unzipcolumn="";
				try{
					unzipcolumn=((String) sqlElement.getAttributeValue("unzipcolumn")).trim();
				}catch(Exception e){}
				if(null==unzipcolumn||"null".equals(unzipcolumn))unzipcolumn="";
				String sqlName="";
				try{
					sqlName=((String) sqlElement.getAttributeValue("name")).trim();
				}catch(Exception e){}
				
				int idx = Integer.parseInt(((String) sqlElement
						.getAttributeValue("index")).trim());
				if(null==sqlName||"".equals(sqlName)){
					sqlName="Sql_"+idx;
				}
				String datasource=(String) sqlElement.getAttributeValue("datasource");
				String userDefineDatasource=(String) sqlElement.getAttributeValue("userDefineDatasource");
				String sqlColums=(String) sqlElement.getAttributeValue("sqlColums");
				((QueryMethodContentXml) methodContentXml)
						.addQuerySql(idx,sqlName, sql,unzipcolumn,datasource,userDefineDatasource,sqlColums);
			}
		} else if ("update".equals(getType())) {
			Element transactionElement = method.getChild("Transaction");
			String callback=transactionElement.getAttributeValue("callback");
			if(callback==null)callback="";
			String datasource=transactionElement.getAttributeValue("datasource");
			if(datasource==null)datasource="";
			String userDefineDatasource=transactionElement.getAttributeValue("userDefineDatasource");
			methodContentXml = new UpdateMethodContentXml();
			((UpdateMethodContentXml) methodContentXml).setCallback(callback);
			((UpdateMethodContentXml) methodContentXml).setDatasource(datasource);
			((UpdateMethodContentXml) methodContentXml).setUserDefineDatasource(userDefineDatasource);
			List<Element> transactions = transactionElement.getChildren();
			for (int i = 0; i < transactions.size(); i++) {
				Element sqlElement = transactions.get(i);
				String sql = sqlElement.getValue();
				int idx = Integer.parseInt(((String) sqlElement
						.getAttributeValue("index")).trim());
				String loopby = sqlElement.getAttributeValue("loopby");
				((UpdateMethodContentXml) methodContentXml).addUpdateSql(idx,
						sql, loopby);
			}

		} else if ("java".equals(getType())) {
			Element javaStaticMethod = method.getChild("JavaStaticMethod");
			methodContentXml = new JavaMethodContentXml();
			((JavaMethodContentXml) methodContentXml)
					.setClassname(javaStaticMethod
							.getAttributeValue("classname"));
			((JavaMethodContentXml) methodContentXml)
					.setStaticMethodName(javaStaticMethod
							.getAttributeValue("staticMethodName"));
		} else {
			throw new Exception("错误的方法类型，无法解析");
		}
	}

	public MethodXml() {

	}

	public void copyMethodParms(List<MethodParmXml> list) {
		methodParms.addAll(list);
		sortMethodParm();
	}

	public void sortMethodParm() {
		MethodParmXml tmpParm;
		for (int i = 1; i < methodParms.size(); i++) {
			for (int j = i; (j > 0)
					&& (methodParms.get(j).getId() < methodParms.get(j - 1)
							.getId()); j--) {
				tmpParm = methodParms.get(j);
				methodParms.set(j, methodParms.get(j - 1));
				methodParms.set(j - 1, tmpParm);
			}
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<MethodParmXml> getMethodParms() {
		return methodParms;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}
	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}
	public String getRemoveMemcachedUserLicense() {
		if("true".equals(RemoveMemcachedUserLicense)){
			RemoveMemcachedUserLicense= "true";
		}else{
			RemoveMemcachedUserLicense= "false";
		}
		return RemoveMemcachedUserLicense;
	}

	public void setRemoveMemcachedUserLicense(String removeMemcachedUserLicense) {
		if("true".equals(removeMemcachedUserLicense)){
			RemoveMemcachedUserLicense = removeMemcachedUserLicense;
		}else{
			RemoveMemcachedUserLicense = "false";
		}
	}
	
	public String getIsMobilApp() {
		if("true".equals(isMobilApp)){
			isMobilApp= "true";
		}else{
			isMobilApp= "false";
		}		
		return isMobilApp;
	}

	public void setIsMobilApp(String isMobilApp) {
		if("true".equals(isMobilApp)){
			this.isMobilApp = "true";
		}else{
			this.isMobilApp = "false";
		}		
	}
	public String getAsyncService() {
		if("true".equals(asyncService)){
			asyncService= "true";
		}else{
			asyncService= "false";
		}		
		return asyncService;
	}

	public void setAsyncService(String asyncService) {
		if("true".equals(asyncService)){
			this.asyncService = "true";
		}else{
			this.asyncService = "false";
		}		
	}

	public MethodContentXml getMethodContentXml() {
		return methodContentXml;
	}

	public void setMethodContentXml(MethodContentXml methodContentXml) {
		this.methodContentXml = methodContentXml;
	}

	public MethodMencacheXml getMethodMencacheXml() {
		return methodMencacheXml;
	}

	public void setMethodMencacheXml(MethodMencacheXml methodMencacheXml) {
		this.methodMencacheXml = methodMencacheXml;
	}

	public StringBuffer toXml() {
		StringBuffer xml = new StringBuffer();
		xml.append("	<Method id=\"" + this.getId() + "\" name=\""
				+ this.getName() + "\" type=\"" + this.getType()
				+ "\" pagination=\"" + this.getPagination() + "\" author=\"" + this.getAuthor()
				+ "\" comment=\"" + this.getComment() + "\" IsMobilApp=\""+this.getIsMobilApp()+"\" AsyncService=\""+this.getAsyncService()+"\" RemoveMemcachedUserLicense=\"" + this.getRemoveMemcachedUserLicense() + "\" date=\""
				+ this.getDate() + "\">\r\n");
		if (methodContentXml != null) {
			xml.append(methodContentXml.toXml());
		}
		xml.append("		<Parms>\r\n");
		for (int i = 0; i < this.getMethodParms().size(); i++) {
			MethodParmXml methodParmXml = this.getMethodParms().get(i);
			xml.append(methodParmXml.toXml());
		}
		xml.append("		</Parms>\r\n");
		if (methodMencacheXml != null) {
			xml.append(methodMencacheXml.toXml());
		}
		xml.append("	</Method>\r\n");
		return xml;
	}
}
